package com.shwread.android.ui.dialog;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;

import java.util.Calendar;

/**
 */
public class GooutDatePickerDialog   extends
        AlertDialog
        implements
        DialogInterface.OnClickListener,
        DatePicker.OnDateChangedListener
{

    private DatePicker            datePicker;
    private Context               context;
    private OnDateTimeSetListener mDateSetListener;

    public interface OnDateTimeSetListener
    {
        void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth);
    }

    public GooutDatePickerDialog(Context context, OnDateTimeSetListener dateSetListener) {
        super(context);
        this.context = context;
        this.mDateSetListener = dateSetListener;

        findViews();

        setDateTime();
        getWindow().setLayout(ViewPager.LayoutParams.MATCH_PARENT, ViewPager.LayoutParams.WRAP_CONTENT);
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setWindowAnimations(android.R.style.Animation_InputMethod);
    }

    void findViews() {
        View view = LayoutInflater.from(context).inflate(com.mywork.R.layout.out_pick_date_time, null);
        datePicker = (DatePicker) view.findViewById(com.mywork.R.id.dpPicker);
        setView(view);

        setButton(BUTTON_POSITIVE, context.getString(com.mywork.R.string.pick_ok), this);
        setButton(BUTTON_NEGATIVE, context.getString(com.mywork.R.string.pick_cancel), this);
    }

    void setDateTime() {
        Calendar c = Calendar.getInstance();
        int mMinYear = c.get(Calendar.YEAR);
        int mMinMonth = c.get(Calendar.MONTH);
        int mMinDay = c.get(Calendar.DAY_OF_MONTH);


        datePicker.init(mMinYear, mMinMonth, mMinDay, new DatePicker.OnDateChangedListener()
        {

            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                // 获取一个日历对象，并初始化为当前选中的时间
                Calendar calendar = Calendar.getInstance();

                calendar.set(year, monthOfYear, dayOfMonth);
            }
        });



    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case BUTTON_POSITIVE:
                if (mDateSetListener != null)
                {
                    mDateSetListener.onDateSet(datePicker, datePicker.getYear(), datePicker.getMonth(), datePicker.getDayOfMonth());
                }
                break;
            case BUTTON_NEGATIVE:
                cancel();
                break;
        }
    }

    @Override
    public void onDateChanged(DatePicker view, int year, int month, int day) {
        datePicker.init(year, month, day, this);
    }

    @Override
    public void show() {
        super.show();
        Button positiveButton = getButton(BUTTON_POSITIVE);
        Button negativeButton = getButton(BUTTON_NEGATIVE);
        positiveButton.setTextColor(context.getResources().getColor(android.R.color.holo_blue_light));
        negativeButton.setTextColor(context.getResources().getColor(android.R.color.holo_blue_light));
    }
}
